package com.hn658.base.message.frontend.action.send.impl;

import java.util.Date;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.hn658.base.message.common.enums.EventPriority;
import com.hn658.base.message.common.enums.EventType;
import com.hn658.base.message.eventbus.event.groupchat.GroupMsgEvent;
import com.hn658.base.message.frontend.action.send.IGroupSendAction;
import com.hn658.base.message.frontend.action.send.vo.SendMessageRequest;
import com.hn658.base.message.frontend.action.send.vo.SendMessageRespose;
import com.hn658.base.message.notification.msg.entity.MessageEO;
import com.hn658.base.message.publish.business.IEventPublishManager;
import com.hn658.framework.common.AbstractBaseService;
import com.hn658.framework.common.IServiceResponse;
import com.hn658.framework.shared.exception.BusinessException;
import com.hn658.framework.shared.utils.UUIDUtils;

/**
 * 
 * 消息服务
 * 
 * @author ztjie
 * @date 2015-11-3 上午10:02:04
 * @since
 * @version
 */
@Controller("groupSendAction")
@Path("group")
@Produces("application/json; charset=UTF-8")
@Consumes("application/json; charset=UTF-8")
public class GroupSendActionImpl extends AbstractBaseService implements
		IGroupSendAction {

	@Autowired
	private IEventPublishManager eventPublishManager;

	@Path("publish")
	@POST
	public IServiceResponse publish(SendMessageRequest request) {
		SendMessageRespose response = new SendMessageRespose();
		MessageEO message = new MessageEO();
		try {
			message.setCreateTime(new Date());
			message.setDeleted(false);
			message.setFromUid(request.getFromUid());
			message.setMsg(request.getMsg());
			message.setMsgType(request.getMsgType());
			message.setAppId(request.getAppId());
			message.setToGroupId(request.getToGroupId());
			message.setUuid(UUIDUtils.getUUID() + "");
			GroupMsgEvent event = new GroupMsgEvent();
			event.setAppId(request.getAppId());
			event.setEventId(UUIDUtils.getUUID());
			event.setType(EventType.NormalGroupChat);
			event.setMessage(message);
			event.setPriority(EventPriority.getPriorityByValue(request.getEvenPriority()));
			event.setIsNeedSave(true);
	        event.setIsOfflineSend(true);
			eventPublishManager.publishGroupMsgEvent(event);
			this.success(response);
		} catch (BusinessException e) {
			this.error(response, e);
		}
		return response;
	}
}
